﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="SetAutoUpdateFlag.cs" company="DHGMS Solutions">
//   Copyright (c) 2012 DHGMS Solutions
//
//   Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
//   The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
//
//   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// </copyright>
// <summary>
//   Checks through the current users steam apps list in their registry
//   settings to ensure that they have auto update enabled.
//   Based on information from http://forums.steamgames.com/forums/showthread.php?t=1769942
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Dhgms.Kettle.Launcher.Model.Task
{
    using System;
    using Microsoft.Win32;

    /// <summary>
    /// Checks through the current users steam apps list in their registry
    /// settings to ensure that they have auto update enabled.
    /// Based on information from http://forums.steamgames.com/forums/showthread.php?t=1769942
    /// </summary>
    public class SetAutoUpdateFlag
    {
        /// <summary>
        /// Entry point for doing the donkey work
        /// </summary>
        public void Execute()
        {
            var appsRootKey = GetSteamAppsRootKey();

            var subKeyNames = appsRootKey.GetSubKeyNames();

            foreach (var subKeyName in subKeyNames)
            {
                CheckApp(appsRootKey, subKeyName);
            }
        }

        /// <summary>
        /// Checks an app to see if auto update is enabled
        /// </summary>
        /// <param name="appsRootKey">
        /// The steam apps root key.
        /// </param>
        /// <param name="subKeysName">
        /// The sub keys name.
        /// </param>
        /// <exception cref="ApplicationException">
        /// Failed to open steam app registy key
        /// </exception>
        private static void CheckApp(RegistryKey appsRootKey, string subKeysName)
        {
            var subkey = appsRootKey.OpenSubKey(subKeysName);
            if (subkey == null)
            {
                throw new ApplicationException("Failed to open steam app registy key");
            }

            // check the app has an auto update key
            const string KeyName = "EnableCacheLoading";

            var enableCacheLoadingValue = subkey.GetValue(KeyName);
            if (enableCacheLoadingValue.GetType() != typeof(int))
            {
                throw new ApplicationException("Registry Value is an unexpected data type.");
            }

            var enableCacheLoading = (int)enableCacheLoadingValue;
            if (enableCacheLoading == 1)
            {
                return;
            }

            subkey.SetValue(KeyName, 1, RegistryValueKind.DWord);
        }

        /// <summary>
        /// Gets the users steam apps registry key
        /// </summary>
        /// <returns>
        /// The users steam apps registry key
        /// </returns>
        /// <exception cref="ApplicationException">
        /// Failed to get the users steam apps registry key
        /// </exception>
        private static RegistryKey GetSteamAppsRootKey()
        {
            const string Path = @"Software\Valve\Steam\Apps";

            var key = Registry.CurrentUser.OpenSubKey(Path);

            if (key == null)
            {
                throw new ApplicationException("Unable to get the user steamapps registry key");
            }

            return key;
        }
    }
}
